<template>
    <div class="Menu-container content-wrapper">

        <el-row :gutter="20">
            <el-col :span="6">
                <el-card class="box-card" :body-style="{ padding: '0px' }">
                    <div class="clearfix card-header">
                        <span><i class="el-icon-menu" style="color:#324057"></i> 菜单管理</span>
                        <el-button @click="handleReload" icon="el-icon-refresh" style="float: right; padding: 5px 0px 0px 3px" type="text"></el-button>
                        <el-button @click="handleDelete" icon="el-icon-delete" style="float: right; padding: 5px 10px 0px 3px" type="text"></el-button>
                        <el-button @click="handleUpdate" icon="el-icon-edit" style="float: right; padding: 5px 0px 0px 3px" type="text"></el-button>
                        <el-button @click="handleCreate" icon="el-icon-plus" style="float: right; padding: 5px 0px 0px 3px" type="text"></el-button>
                    </div>
                    <el-tree :data="tree" node-key="id" ref="tree"
                             v-loading="loading"
                             highlight-current
                             :check-strictly="true"
                             :default-expand-all="true"
                             :props="defaultProps"
                             :default-expanded-keys="expandedKey"
                             @node-click="handleNodeClick"
                    ></el-tree>
                </el-card>
            </el-col>

            <el-col :span="18">
                <table>
                    <tr>
                        <td align="right">所属上级：</td>
                        <td>{{detail.parentName == null ? '无上级' : detail.parentName}}</td>
                    </tr>
                    <tr>
                        <td align="right">菜单名称：</td>
                        <td>{{detail.name}}</td>
                    </tr>
                    <tr>
                        <td align="right">菜单编码：</td>
                        <td>{{detail.code}}</td>
                    </tr>
                    <tr>
                        <td align="right">URL地址：</td>
                        <td>{{detail.url}}</td>
                    </tr>
                    <tr>
                        <td align="right">类型：</td>
                        <td>
                            <el-tag type="primary" size="mini" v-if="detail.type === 1">目录</el-tag>
                            <el-tag type="danger" size="mini" v-if="detail.type === 2">菜单</el-tag>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">状态：</td>
                        <td>
                            <el-tag type="success" size="mini" v-if="detail.status === 1">正常</el-tag>
                            <el-tag type="danger" size="mini" v-if="detail.status === 2">禁用</el-tag>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">排序：</td>
                        <td>{{detail.orders}}</td>
                    </tr>
                    <tr>
                        <td align="right">备注：</td>
                        <td>{{detail.remarks}}</td>
                    </tr>
                </table>
            </el-col>
        </el-row>

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" @close="handleCloseDialog">
            <el-row>
                <el-col :md="6" :lg="6">
                    <el-tree :data="tree"
                             node-key="id"
                             highlight-current
                             :default-expanded-keys="expandedKeyEdit"
                             :props="defaultProps"
                             @node-click="handleSetParent"></el-tree>
                </el-col>
                <el-col :md="6" :lg="6">
                    <el-form :model="form" ref="form" :rules="rules" labelPosition="right" labelWidth="80px" style="width: 400px">

                        <el-form-item label="所属上级">
                            <el-input v-model="form.parentName" placeholder="请选择所属上级" readonly >
                                <i class="el-icon-close el-input__icon" slot="suffix" @click="handleIconClick"></i>
                            </el-input>
                        </el-form-item>

                        <el-form-item label="菜单名称" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>

                        <el-form-item label="菜单编码" prop="code">
                            <el-input v-model="form.code"></el-input>
                        </el-form-item>

                        <el-form-item label="URL地址">
                            <el-input v-model="form.url"></el-input>
                        </el-form-item>

                        <el-form-item label="排序">
                            <el-input-number v-model="form.orders" :min="1"></el-input-number>
                        </el-form-item>

                        <el-form-item label="类型">
                            <el-radio-group v-model.number="form.type">
                                <el-radio class="radio" :label="1">目录</el-radio>
                                <el-radio class="radio" :label="2">菜单</el-radio>
                            </el-radio-group>
                        </el-form-item>

                        <el-form-item label="状态">
                            <el-radio-group v-model.number="form.status">
                                <el-radio class="radio" :label="1">正常</el-radio>
                                <el-radio class="radio" :label="2">禁用</el-radio>
                            </el-radio-group>
                        </el-form-item>

                        <el-form-item label="备注">
                            <el-input
                                    type="textarea"
                                    :autosize="{ minRows: 2, maxRows: 4}"
                                    placeholder="请输入内容"
                                    v-model="form.remarks">
                            </el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button v-if="dialogStatus == 'create'" type="primary" @click="create">确 定</el-button>
                <el-button v-else="" type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import { created, updated, deleted, selectOne, selectTree } from '@/api/upms/menu'
    export default {
        name: 'Menu',
        data() {
            return {
                tree: [],
                loading: false,
                defaultProps: {
                    label: 'name',
                    children: 'children'
                },
                textMap: {
                    create: '新增',
                    update: '编辑'
                },
                dialogStatus: '',
                dialogFormVisible: false,
                form: {
                    id: '',
                    pid: '',
                    name: '',
                    type: 1,
                    code: '',
                    url: '',
                    status: 1,
                    orders: '',
                    remarks: '',
                    parentName: ''
                },
                detail: {
                    id: '',
                    pid: '',
                    name: '',
                    type: '',
                    code: '',
                    url: '',
                    status: '',
                    orders: '',
                    remarks: '',
                    parentName: ''
                },
                expandedKey: [1],
                expandedKeyEdit: [1],
                rules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '请输入编码', trigger: 'blur' }
                    ]
                }
            }
        },
        created() {
            this.getTreeData()
        },
        methods: {
            handleNodeClick(data, node) {
                this.detail = data
                this.detail['parentName'] = node.parent.data.name
            },
            handleCreate() {//添加数据窗口
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            handleUpdate() {//修改数据窗口
                if(this.detail.id) {
                    selectOne(this.detail.id).then(response => {
                        this.dialogStatus = 'update'
                        this.dialogFormVisible = true
                        this.form = Object.assign({}, response.data)
                        this.form['parentName'] = this.detail.parentName
                        this.expandedKeyEdit = [this.detail.id]
                    })
                } else {
                    this.$message({ type: 'warning', message: '请选择需要修改的菜单' })
                }
            },
            handleDelete() {//删除数据
                if(this.detail.id) {
                    this.$confirm('确定要删除？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        deleted(this.detail.id).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTreeData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })
                    }).catch(() => {
                        this.$message({ type: 'info', message: '已取消删除' })
                    })
                } else {
                    this.$message({ type: 'warning', message: '请选择需要删除的菜单' })
                }
            },
            create() {//添加数据
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        created(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTreeData()
                                this.dialogFormVisible = false
                            } else {
                                this.$notify({
                                    title: '操作失败', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })
                    }
                })
            },
            update() {//修改数据
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        updated(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })

                                this.detail = Object.assign({}, this.form)
                                this.getTreeData()
                                this.dialogFormVisible = false
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })
                    }
                })
            },
            getTreeData() {//获取数据列表
                this.loading = true
                selectTree().then(response => {
                    this.tree = response.data
                    this.loading = false
                }).catch(error => {
                    console.log(error)
                    this.loading = false
                })
            },
            handleSetParent(data, node) {
                var shut = false
                var sf = this.form

                //判断是否选择自己作为上级
                if(this.form.id == data.id) {
                    this.$message({
                        title: '提示', message: '选择所属上级有误',
                        type: 'info', duration: 2000
                    })
                    return
                }
                //判断选择的上级是否当前修改的上级
                res(node)
                function res(node) {
                    if(!shut) {
                        var n = node.parent
                        if(null != n) {
                            if(sf.id == n.data.id || sf.id == n.data.pid) {
                                shut = true
                            }
                            res(n)
                        }
                    }
                }
                if(shut == true) {
                    this.$message({
                        title: '提示', message: '选择所属上级有误',
                        type: 'warning', duration: 2000
                    })
                    return
                }

                this.form = Object.assign({}, this.form)
                this.form['pid'] = data.id
                this.form['parentName'] = data.name
            },
            handleIconClick(ev) {
                this.form = Object.assign({}, this.form)
                this.form['pid'] = ''
                this.form['parentName'] = ''
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleReload(){
                this.getTreeData()
                this.detail = {
                    id: '',
                    pid: '',
                    name: '',
                    type: '',
                    code: '',
                    url: '',
                    status: '',
                    orders: '',
                    remarks: '',
                    parentName: ''
                }
            },
            resetForm() {
                if(this.$refs['form']) {
                    this.$refs['form'].resetFields()
                }
                this.form = {
                    id: '',
                    pid: '',
                    name: '',
                    type: 1,
                    code: '',
                    url: '',
                    status: 1,
                    orders: '',
                    remarks: '',
                    parentName: ''
                }

            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>
    .card-header{
        border-bottom: 1px solid #d1dbe5; padding: 15px 20px 15px 7px;
    }
    .el-card__body{
        .el-tree{
            border: 0px;
        }
    }
</style>